package ms.unicorn.erml.helper;
import ms.unicorn.Element;
import ms.unicorn.erml.ERML;
import ms.unicorn.erml.edit.parts.SubPartCompartmentEditPart;
import ms.unicorn.erml.edit.parts.SubPartEditPart;
import ms.unicorn.erml.figures.SubPartFigure;
import ms.unicorn.erml.styles.ErmlStyle.CollapsableConstraint;
import ms.unicorn.erml.styles.ErmlStyle.ErmlStyleFactory;
import ms.unicorn.modellanguage.LanguageElement;
import ms.unicorn.modellanguage.LanguageEntityEditHeperAdvice;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.gef.EditPart;
import org.eclipse.gmf.runtime.diagram.core.preferences.PreferencesHint;
import org.eclipse.gmf.runtime.diagram.core.util.ViewUtil;
import org.eclipse.gmf.runtime.notation.BasicCompartment;
import org.eclipse.gmf.runtime.notation.NotationFactory;
import org.eclipse.gmf.runtime.notation.View;


public class SubPartEditHelperAdvice extends LanguageEntityEditHeperAdvice {

	public SubPartEditHelperAdvice() {
		editPartMap.put(ENTITY_ID, SubPartEditPart.class);
		editPartMap.put(ERML.SUBPART_COMPARTMENT_ID, SubPartCompartmentEditPart.class);
	}
	
	@Override
	public boolean acceptChildren(LanguageElement element) {
		return element != ERML.SubPart;
	}
	
	@Override
	protected IFigure createFigureElement(Element element, String figureElement, EditPart elementEditPart) {
		if (FIGURE_ELEMENT_PRIMARY_SHAPE.equals(figureElement)) {
			SubPartFigure figure = new SubPartFigure((SubPartEditPart) elementEditPart);
			return figure;
		}
		else
			return super.createFigureElement(element, figureElement, elementEditPart);
	}
		
	@SuppressWarnings("unchecked")
	@Override
	public View createView(IAdaptable semanticAdapter, View containerView,
			String semanticHint, int index, boolean persisted,
			PreferencesHint preferencesHint) {
		View view = null;
		
		if (semanticHint == null) {
			view = super.createView(semanticAdapter, containerView, semanticHint, index,
					persisted, preferencesHint);
			
			CollapsableConstraint sizes = ErmlStyleFactory.eINSTANCE.createCollapsableConstraint();
			Dimension size = ERML.SubPart.getDefaultSize();
			sizes.setCollapsedSize(size.width, size.height);
			sizes.setExpandedSize(300, 300);
			
			view.getStyles().add(sizes);
			
			BasicCompartment compartment = NotationFactory.eINSTANCE.createBasicCompartment();
			compartment.setCollapsed(true);
			compartment.setType(ERML.SUBPART_COMPARTMENT_ID);
			
			ViewUtil.insertChildView(view, compartment, ViewUtil.APPEND, true);
		}
		
		return view;
	}	
}
